package edu.dali.enterprise_test.controller;

import edu.dali.enterprise_test.model.dto.Menus;
import edu.dali.enterprise_test.model.vo.SysRole;
import edu.dali.enterprise_test.model.vo.UserInfo;
import edu.dali.enterprise_test.service.auth.AuthService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value={"/admin" , "/"})
public class AdminControler {

    @Autowired
    private AuthService authService;


    @RequestMapping(value = {"index" , "/"})
    public String index(Model model) {
        UserInfo u = (UserInfo) SecurityUtils.getSubject().getPrincipal();
        model.addAttribute("user" , u);
        return "admin/index";
    }

    @PostMapping("getMenus")
    @ResponseBody
    public List<Menus> getMenus() {

        UserInfo u = (UserInfo) SecurityUtils.getSubject().getPrincipal();
        List<SysRole> roles = u.getRoles();

        boolean superFlag = false;
        for (SysRole r:roles) {
            if (r.getRole().equals("super")) superFlag = true;
        }

        List<Menus> ret = new ArrayList<>();
//        String fp = eInvoiceService.stock(taxcode);
        Menus headers1 = new Menus();
        headers1.setIcon("");
        headers1.setId("9000");
//        headers1.setText("剩余:"+fp+"张");
        headers1.isHeader();
        ret.add(headers1);


        Menus first = new Menus();
        first.setId("9004");
        first.setText("菜谱管理");
        first.setIcon("fa fa-lock");
        first.setOpen(true);
        List<Menus> firstList = new ArrayList<>();
        Menus first1 = new Menus();
        first1.setId("90041");
        first1.setText("菜谱列表");
        first1.setIcon("fa fa-users");
        first1.setUrl("/menu/index");
        first1.setTargetType("iframe-tab");
        firstList.add(first1);

        Menus first2 = new Menus();
        first2.setId("90042");
        first2.setText("主体分类列表");
        first2.setIcon("fa fa-birthday-cake");
        first2.setUrl("/mainc/index");
        first2.setTargetType("iframe-tab");
        firstList.add(first2);

        Menus first3 = new Menus();
        first3.setId("90043");
        first3.setText("详细分类列表");
        first3.setIcon("fa fa-birthday-cake");
        first3.setUrl("/subc/index");
        first3.setTargetType("iframe-tab");
        firstList.add(first3);

        first.setChildren(firstList);
        ret.add(first);

        Menus income = new Menus();
        income.setId("9028");
        income.setText("客户管理");
        income.setIcon("fa fa-lock");
        List<Menus> incomeList= new ArrayList<>();
        Menus income1 = new Menus();
        income1.setId("90281");
        income1.setText("客户列表");
        income1.setIcon("fa fa-users");
        income1.setUrl("/customer/index");
        income1.setTargetType("iframe-tab");
        incomeList.add(income1);
        Menus income2 = new Menus();
        income2.setId("90282");
        income2.setText("销售管理");
        income2.setIcon("fa fa-users");
        income2.setUrl("/sale/index");
        income2.setTargetType("iframe-tab");
        incomeList.add(income2);

        income.setChildren(incomeList);
        ret.add(income);

        if (superFlag) {
            Menus second = new Menus();
            second.setId("9010");
            second.setText("系统管理");
            second.setIcon("fa fa-lock");
            List<Menus> secondList = new ArrayList<>();

//            Menus second1 = new Menus();
//            second1.setId("900101");
//            second1.setText("商铺管理");
//            second1.setIcon("fa fa-users");
//            second1.setUrl("/store/index");
//            second1.setTargetType("iframe-tab");
//            secondList.add(second1);

            Menus second2 = new Menus();
            second2.setId("900102");
            second2.setText("用户管理");
            second2.setIcon("fa fa-users");
            second2.setUrl("/user/index");
            second2.setTargetType("iframe-tab");
            secondList.add(second2);

            Menus second3 = new Menus();
            second3.setId("900103");
            second3.setText("角色管理");
            second3.setIcon("fa fa-users");
            second3.setUrl("/role/index");
            second3.setTargetType("iframe-tab");
            secondList.add(second3);

            Menus second4 = new Menus();
            second4.setId("900104");
            second4.setText("权限管理");
            second4.setIcon("fa fa-users");
            second4.setUrl("/permission/index");
            second4.setTargetType("iframe-tab");
            secondList.add(second4);

            second.setChildren(secondList);
            ret.add(second);
        }

        return ret;
    }

    /**
     * 修改密码对话框
     * @return
     */
    @RequestMapping("pwdModify")
    public String pwdModify() {

        return "admin/dialog/pwdModify";
    }

    /**
     * 修改密码处理程序
     * @param orignPwd
     * @param newPwd
     * @return
     */
    @PostMapping("pwdProc")
    @ResponseBody
    public Map<String , String> pwdProc(String orignPwd , String newPwd) {
        Subject subject = SecurityUtils.getSubject();
        UserInfo userInfo = (UserInfo) subject.getPrincipal();
        long uid = userInfo.getUid();

        authService.updatePwd(String.valueOf(uid) , newPwd);
        Map<String , String> map = new HashMap<>();
        map.put("code" , "0");
        map.put("msg" , "密码修改成功");
        return map;
    }
}
